<?php

namespace App\Http\Controllers\Api\Room;

use App\Http\Controllers\Controller;
use App\Libs\Strategy\Context;
use App\Models\Rooms\GuessinGoods;
use App\Models\Rooms\RoomsLevel;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    private $goods;

    public function __construct(){
        $this->goods = new Context(new GuessinGoods());
    }

    public function index(Request $request)
    {
        $roomLevels = RoomsLevel::get();

        $goods = $this->goods->display($request->limit,function($query) use($request){
            if($request->keyword)
            {
                $query->where('room_goods_name','like','%'.$request->keyword.'%');
            }
            if($request->is_show)
            {
               $query->where('is_show',$request->is_show);
            }
            if($request->is_hot)
            {
               $query->where('is_hot',$request->is_hot);
            }
            if($request->level)
            {
               $query->where('room_id','like','%,'.$request->level.',%');
            }
        });
        return $this->success(['goods'=>$goods, 'roomLevel' => $roomLevels]);
    }

    public function create()
    {
        $cates = GoodsLogic::getCate();
        $roomLevels = RoomLogic::getRooms();
        return view('room.guessin_goods.add',compact('roomLevels','cates'));
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'room_goods_img' => 'required',
            'room_id' => 'required',
            'room_goods_content' => 'required',
            'room_goods_golden' => 'required',
            'room_goods_rules' => 'required',
        ],[
            'room_goods_img.required' => '商品图片不能为空',
            'room_id.required' => '请选择商品等级',
            'room_goods_content.required' => '商品描述不能为空',
            'room_goods_rules.required' => '开奖规则不能为空',
            'room_goods_golden.required' => '请设置商品等级金豆',
        ]);
        if($this->goods->create($request->only('room_id','buy_back_price','room_goods_name','room_goods_price','room_goods_img','is_show','room_goods_golden','sort','goods_cate_id','room_goods_rules','room_goods_content'))){
             return $this->success(['message'=>'添加成功']);
        };
        return $this->error('添加失败');
    }


    public function update(Request $request,$id)
    {
        $this->validate($request,[
            'room_goods_img' => 'required',
            'room_id' => 'required',
            'room_goods_content' => 'required',
            'room_goods_golden' => 'required',
            'room_goods_rules' => 'required',
        ],[
            'room_goods_img.required' => '商品图片不能为空',
            'room_id.required' => '请选择商品等级',
            'room_goods_content.required' => '商品描述不能为空',
            'room_goods_rules.required' => '开奖规则不能为空',
            'room_goods_golden.required' => '请设置商品等级金豆',
        ]);
        if($this->goods->update($id,$request->only('room_id','buy_back_price','room_goods_name','room_goods_price','room_goods_img','is_show','room_goods_golden','sort','goods_cate_id','room_goods_rules','room_goods_content'))){
             return $this->success(['message'=>'编辑成功']);
        };
        return $this->error('编辑失败');
    }

    public function show($id)
    {
        return $this->success($this->goods->find($id));
    }

    public function destroy($id)
    {
        if($this->goods->find($id)->delete($id))
        {
            return $this->success(['message'=>'删除成功']);
        }
        return $this->error('删除失败');
    }
}
